// miniprogram/pages/rumor-background-tester/imgUpload/imgUpload.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lost_code: "",
    img: [],
    count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url : "http://localhost:8081/api/getLostCode",
      method: "GET",
      data: {
        
      },
      success: function (res) {
        if(res.data != null){
          that.setData({
            lost_code: res.data
          })
        }
        console.log(that.data.lost_code);
        wx.showToast({
          title: '请求成功！',
          icon: 'success',
          duration: 2000
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  imgChoose: function(){
    var that = this
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        that.setData({
          img: that.data.img.concat(res.tempFilePaths),
          count: that.data.count + 1
        });
        // wx.uploadFile({
        //   url: 'http://localhost:8080/rumorImgUpload',//自己的接口地址
        //   filePath: that.data.img[that.data.count-1],
        //   name: 'content',
        //   header: {
        //     "Content-Type": "multipart/form-data",
        //     'accept': 'application/json',
        //   //  'Authorization': 'okgoodit'//若有token，此处换上你的token，没有的话省略
        //   },
        //   formData:({//上传图片所要携带的参数
        //     open_id: "op2en_id",
        //     rand_code: that.data.rand_code,
        //     filename: "" + that.data.count
        //   }),
        //   success: function (res) {
        //     console.log(res)
        //     if (res) {
        //       console.log("返回的参数信息" + res.data)
        //       wx.showToast({
        //         title: '已提交发布！',
        //         duration: 3000
        //       });
        //     }
        //   }
        // })
      }
    })
  },

  imgUpload: function(){
    var that = this
    setTimeout( function() {
    for(var i = 0; i < that.data.img.length; i++){
      var path = that.data.img[i]
      wx.uploadFile({
        url: 'http://localhost:8081/api/lostImgUpload',//自己的接口地址
        filePath: path,
        name: 'content',
        header: {
          "Content-Type": "multipart/form-data",
          'accept': 'application/json',
        //  'Authorization': 'okgoodit'//若有token，此处换上你的token，没有的话省略
        },
        formData:({//上传图片所要携带的参数
          username: "op2en_id",
          lost_code: "lcGu0M5o", //that.data.lost_code,
          filename: "0" + i
        }),
        success: function (res) {
          console.log(res)
          if (res) {
            console.log("返回的参数信息" + res.data)
            wx.showToast({
              title: '已提交发布！',
              duration: 3000
            });
          }
        }
      })
    }
  },1000)


  },

  contributeSubmit: function(){
    wx.request({
      url : "http://localhost:8081/api/lostSubmit",
      method: "POST",
      data: {
        username: "op2en_id",
        lost_code: "lcGu0M5o",
        remark: "1"
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res.data);
        wx.showToast({
          title: '请求成功！',
          icon: 'success',
          duration: 2000
        })
      },
    })
  },

  imgDelete: function(){
    wx.request({
      url : "http://localhost:8081/api/deleteLostImg",
      method: "POST",
      data: {
        username: "op2en_id",
        lost_code: "lcGu0M5o",
        filename: "01"
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res.data);
        wx.showToast({
          title: '请求成功！',
          icon: 'success',
          duration: 2000
        })
      },
    })
  },
})